package com.fly.handler;

import com.fly.domain.Department;
import com.fly.service.DepartmentService;
import com.fly.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.web.reactive.function.BodyInserters.fromObject;

/**
 * @author Fly
 * @Description
 * @Date Created in 15:31 2018/1/25
 * @Modified by
 */
@Component
public class DepartmentHandler {

    private final DepartmentService departmentService;

    @Autowired
    public DepartmentHandler(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 保存
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> save(ServerRequest serverRequest){
        Mono<Department> departmentMono = serverRequest.bodyToMono(Department.class);
        Response response = new Response(true,"保存成功",departmentService.saveDepartment(departmentMono.block()));
        return ServerResponse.ok().body(fromObject(response));
    }

    /**
     * 根据id获取
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> getDepartment(ServerRequest serverRequest){
        Long id = Long.valueOf(serverRequest.pathVariable("id"));
        Department department = departmentService.findById(id);
        return ServerResponse.ok().body(fromObject(department));
    }

    /**
     * 获取组织架构树
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> getTreeData(ServerRequest serverRequest){
        Long id = Long.valueOf(serverRequest.pathVariable("id"));
        Department department = departmentService.findById(id);
        List<Department> departmentList = new ArrayList<>();
        departmentList.add(department);
        return ServerResponse.ok().body(Flux.fromIterable(departmentList),Department.class);
    }

    /**
     * 根据名称查找
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> filterData(ServerRequest serverRequest){
        String name = serverRequest.pathVariable("name");
        List<Department> departmentList = departmentService.findDepartmentsByName(name);
        return ServerResponse.ok().body(Flux.fromIterable(departmentList),Department.class);
    }
    /**
     * 查询列表
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> list(ServerRequest serverRequest){
        Flux<Department> departmentFlux = Flux.fromIterable(departmentService.findAll());
        return ServerResponse.ok().body(departmentFlux,Department.class);
    }

    /**
     * 删除用户
     * @param serverRequest
     * @return
     */
    public Mono<ServerResponse> remove(ServerRequest serverRequest){
        Long id = Long.valueOf(serverRequest.pathVariable("id"));
        departmentService.removeDepartment(id);
        Response response = new Response(true,"删除成功",null);
        return ServerResponse.ok().body(fromObject(response));
    }
}
